<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="name" @tab-change="tabChange">
				<el-tab-pane v-for="item in $tm('finance.approval.name')" :key="item" :label="item[0]" :name="item[1]"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header style="height: auto;">
			<sc-select-filter ref="selectFilter" :data="filterData" :label-width="80"
							  @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<div :style="{width: '70px'}"><label>创建时间从:</label></div>
				<el-date-picker v-model="date.createTimeFrom" type="date" @change="dateFilter"></el-date-picker>
				<div :style="{width: '50px','padding-left': '20px'}"><label>至:</label></div>
				<el-date-picker v-model="date.createTimeTo" type="date" @change="dateFilter"></el-date-picker>
			</div>
		</el-header>
		<el-header>
<!--			<div class="left-panel">-->
<!--				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>-->
				<!--				<el-button type="danger" plain icon="el-icon-delete"></el-button>-->
<!--			</div>-->
			<div>
				<div class="left-panel">
					<el-input v-model="search.keyword" placeholder="关键词"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
					<el-button type="success" icon="el-icon-delete" @click="reset">清空</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe>
				<!--				<el-table-column type="selection" width="50"></el-table-column>-->
				<el-table-column label="审批名称" prop="name" width="100"></el-table-column>
				<el-table-column label="审批编号" prop="number" width="150"></el-table-column>
				<el-table-column label="金额" prop="amount" width="100"></el-table-column>
				<el-table-column label="申请单状态" prop="state" width="100">
					<template  #default="scope">
						<div v-for="(item, index) in $tm('finance.approval.state')" :key="index">
						<el-tag  v-if="scope.row.state===item.value" :type="item.type">{{item.label}}</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column label="申请人" prop="applier" width="120"></el-table-column>
				<el-table-column label="最后审批人" prop="lastApproval" width="120"/>
				<el-table-column label="创建时间" prop="createDate" width="150"/>
				<el-table-column label="更新时间" prop="lastApplyTime" width="150"/>
				<el-table-column label="操作" fixed="right" align="right" width="160">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row)">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
<!--							<el-button text type="primary" size="small" @click="delete_row(scope.row)">删除</el-button>-->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
				 @closed="dialog.save=false"></save-dialog>

</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'

export default {
	name: 'approval',
	components: {
		scSelectFilter,
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			date: {
				createTimeFrom: {},
				createTimeTo: {},
			},
			name: "",
			filterData: [
				{
					title: "审批状态",
					key: "state",
					multiple: true,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "审批中",
							value: "1"
						},
						{
							label: "已通过",
							value: "2"
						},
						{
							label: "已驳回",
							value: "3"
						},
						{
							label: "已撤销",
							value: "4"
						},
						{
							label: "通过后撤销",
							value: "6"
						},
						{
							label: "已删除",
							value: "7"
						},
						{
							label: "已支付",
							value: "10"
						}
					]
				}
			],
			list: {
				apiObj: this.$API.finance.approval.page
			},
			search: {
				keyword: ""
			}
		}
	},
	methods: {
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//日期搜索
		dateFilter() {
			this.$refs.table.upData({
				createTimeFrom: this.$TOOL.dateFormat(this.date.createTimeFrom,"yyyy-MM-dd"),
				createTimeTo: this.$TOOL.dateFormat(this.date.createTimeTo, "yyyy-MM-dd"),
			})
		},
		//标签切换
		tabChange(name) {
			const params = {
				name: name
			};
			this.$refs.table.upData(params)
		},
		filterChange(data) {
			this.$refs.table.upData(data)
		},
		// 重置搜索条件
		reset() {
			this.$refs.selectFilter.reset();
			this.search.keyword = "";
			this.date.createTimeFrom = {};
			this.date.createTimeTo = {};
			// 重载数据，只保留
			this.$refs.table.reload({
				name: this.name
			});
		},//本地更新数据
		handleSaveSuccess() {
			this.$refs.table.refresh()
		},
		//窗口编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//窗口新增
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		delete_row(row) {
			console.info(row)
			// todo 删除
			// 刷新页面
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
